﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.DAL;
using KBManagement.Library;

namespace KBManagement
{
    public partial class EditQtityComponent : Form
    {
        private FoodComponent foodComponent;
        KBSQLClassDataContext context;
        public EditQtityComponent(FoodComponent foodComponent)
        {
            context = Utils.CreateInstance().GetDBContext();
            this.foodComponent = foodComponent;
            InitializeComponent();
            txtQuantity.Text = foodComponent.ComQuantilty.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            FoodComponent comp = context.FoodComponents.SingleOrDefault(x => x.ComId == foodComponent.ComId);
            int quantity = Convert.ToInt32(txtQuantity.Text);
            if (quantity > 0)
            {
                comp.ComQuantilty = Convert.ToInt32(txtQuantity.Text);
            }
            else if (quantity <= 0)
            {
                context.FoodComponents.DeleteOnSubmit(comp);
            }
            context.SubmitChanges();
            try
            {
                Utils.CreateInstanceLog().TransactionLogCommon("Nhân viên " + AdminSession.admin.UserName + " Sửa số lượng " + comp.StoreTemp.Store.S_Name + " thành " + quantity.ToString() + " " + comp.StoreTemp.Store.Unit + " vào lúc " + DateTime.Now);
            }catch(Exception)
            {
                    
            }
            Utils.CreateInstance().NotificationInforObserver();
            Close();
        }
    }
}
